<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class AuthenticationController extends AdminController {
    /*手机号码*/
    public function mobile(){
        $title  = I('get.title');
        $status = I('get.status');
        $map =array();
        if(isset($status)){
            $map['status'] = $status;
        }
        if(isset($title)){
            if(strlen($title)>10){
                $map['mobile'] = $title;
            }else{
                $map['uid'] = $title;
            }
        }

        $list   =   $this->lists('Mobileinfo', $map,'`updatetime` desc');
        $this->assign('_list', $list);
        $this->meta_title = '手机认证';
        $this->display();
    }
    public function mobileview($id = 0){
        empty($id) && $this->error('参数错误！');
        $info = M('Mobileinfo')->field(true)->find($id);
        $info['content'] = json_decode($info['content'],true);
        $this->assign('info', $info);//print_r($info);exit;
        $this->meta_title = '手机认证详细';
        $this->display('mobile_view');
    }
    function pic_base64($pic_url){
        $img = base64_decode($pic_url);
        Header( "Content-type: image/jpeg");//直接输出显示jpg格式图片
        echo $img;exit;
    }
    /**
     * 学信认证
     */
    public function xuexin(){
        $title  = I('get.title');
        $status = I('get.status');
        $map =array();
        if(isset($status)){
            $map['status'] = $status;
        }
        if(isset($title)){
            if(is_numeric($title)){
                $map['uid'] = $title;
            }else{
                $map['realname'] = $title;
            }
        }

        $list   =   $this->lists('Chsi', $map);
        $this->assign('_list', $list);
        $this->meta_title = '学信认证';
        $this->display();
    }
    /**
     * 学信详细
     */
    public function xuexinView($id = 0){
        empty($id) && $this->error('参数错误！');
        $info = M('Chsi')->field(true)->find($id);

        $this->assign('info', $info);
        $this->meta_title = '学信认证详细';
        $this->display('xuexin_view');
    }

    public function xuexindelete($id = 0){
        empty($id) && $this->error('参数错误！');

        $info = M('Chsi')->where('uid='.$id)->delete();
        if($info===1){
          $this->success('修改成功',U('xuexin'));
        }else{
          $this->error('修改失败');
        }

    }

    /**
     *实名认证
     */
    public function realname(){
        $title  = I('get.title');
        $status = I('get.status');
        $map =array();
        if(isset($status)){
            $map['status'] = $status;
        }
        if(isset($title)){
            if(is_numeric($title)){
                $map['uid'] = $title;
            }else{
                $map['realname'] = $title;
            }
        }
        $list   =   $this->lists('Realname', $map);
        $this->assign('_list', $list);
        $this->meta_title = '实名认证';
        $this->display();
    }
    /**
     * 实名认证详细
     */
    public function realnameview($id = 0){

        empty($id) && $this->error('参数错误！');
        $info = M('Realname')->field(true)->find($id);

        $this->assign('info', $info);
        $this->meta_title = '学信认证详细';
        $this->display('realnameview');
    }



    public function realnamevdelete($id = 0){

          empty($id) && $this->error('参数错误！');
          // print_r($id);die;
          $info = M('Realname')->where("uid=".$id)->delete();
          $this->success('删除成功',U('realname'));
    }



    public function realnameUpdate(){
      if(IS_POST){
        $trl=D('Realname')->where('identityNo='.I('post.identityNo'))->save(['realname'=>I('post.realname')]);
        if($trl===1){
          print_r('修改成功');
        }else{
          print_r("修改失败");
        }
      }
    }

    //bank 绑定银行卡
    public function bank(){
        $title  = I('get.title');
        $status = I('get.status');
        $map =array();
        if(isset($status)){
            $map['status'] = $status;
        }
        if(isset($title)){
            if(is_numeric($title)){
                $map['uid'] = $title;
            }else{
                $map['name'] = $title;
            }
        }

        $list   =   $this->lists('BankCard', $map);
        $this->assign('_list', $list);
        $this->meta_title = '银行卡认证';
        $this->display();
    }
    /*
     *     银行更新
     *   */
     public function bankupdate(){

         $model = M('BankCard');
         if(IS_POST){
            $openbank   = I('post.openbank');
            $province   = I('post.province');
            $city       = I('post.city');
            $subbranch  = I('post.subbranch');
            $name           = I('post.name');
            $card_number    = I('post.card_number');
            $mobile         = I('post.mobile');
            $id         = I('post.id');
             $data = [
                'openbank'  =>$openbank,
                'province'  =>$province,
                'city'      =>$city,
                'subbranch' =>$subbranch,
                'name'      =>$name,
                'card_number'=>$card_number,
                'mobile'    =>$mobile,
             ];
             $lists = $model->where("id={$id}")->save($data);
             if($lists){
                 $this->success('修改成功',U('bank'));
             }else{
                 $this->error('修改失败');
             }
         }else{
             $id = I('id');
             $info = $model->where("id={$id}")->find();
             $this->assign('info', $info);
             $this->meta_title = '银行卡修改';
             $this->display();
         }
     }

    public function zhima(){
        $map =array();

        $list   =   $this->lists('BankCard', $map);
        $this->assign('_list', $list);
        $this->meta_title = '芝麻认证';
        $this->display();
    }

    //获取数据
    public function getinfo(){
           $type = I('post.type');
           $id   = I('post.uid');


           if($type=='mobile'){
              $trl =  D('Mobileinfo')->automatic($id);
              // print_r($trl);die;
               if($trl==true){
                    print('获取数据成功');
               }else{
                   print('获取数据失败，稍后再试');
               }
               
           }
          
           // $type = I('get.type');
           // $id   = I('get.id');
           // print_r(I('get.'));die;
           // if($type=='mobile'){
           //    $trl =  D('Mobileinfo')->automatic($id);
           //     if($trl==true){
           //          alert('获取数据成功');
           //     }else{
           //         alert('获取数据失败，稍后在试');
           //     }
           // }

    }
    // 一键获取数据
   public function get_allinfo(){
    
        $vo = D('Mobileinfo')->where('status=-1')->field('uid,token')->select();
// print_r($vo);die;
        // $info2=array();
        $api = new \Common\Extend\Api\ApiClient();
        $api = $api->mobile();
        $api->method            = 'mobile_report/v1/task/report';
        $num=0;
        foreach ($vo as $key => $v) {
            $api->token= $v['token'];
            $info2=$api->Send('post','json')->data();
            // print_r($info2);
                    if($info2['code']==0000){

                        $basicInfoCheck = $info2['data']['basicInfoCheck'];
                        $status = 0;
                        if(!empty($basicInfoCheck)){
                            foreach($basicInfoCheck as $val){
                                if($val['item'] =='idcard_match'){
                                    if($val['result'] ==1 || $val['result']==2){
                                        $status = 2;
                                    }
                                }
                            }
                        }
                        $data = [
                            'uid'       => $v['uid'],
                            'mobile'    => $info2['data']['basicInfo']['mobile'],
                            'name'      => $info2['data']['basicInfo']['name'],
                            'idCard'    => $info2['data']['basicInfo']['identityNo'],
                            'address'   => $info2['data']['basicInfo']['nativeAddress'],
                            'register'  => $info2['data']['basicInfo']['regTime'],
                            'content'   => json_encode($info2['data']),
                            'status'    => $status,
                        ];
                        // print_r($data);die;
                        D('Mobileinfo')->where('uid='.$v['uid'])->save($data);
                        $num++;

                    }
        }
        print_r("获取总数：".count($vo)."，成功：".$num);
        die;

        
    }
    /*
     *    审核通过或不通过  
     */
    public function change(){
        $type   = I('get.type');
        $method = I('get.method');
        $id     = I('get.id');
        $list   = false;
        switch ($type)
        {
            case 'mobile':
              $list =  D('Mobileinfo')->where("uid={$id}")->save(['status'=>$method]);
            break;
            case 'realname':
                $list =  D('Realname')->where("uid={$id}")->save(['status'=>$method]);
            break;
            case 'bank':
                $list =  D('BankCard')->where("uid={$id}")->save(['status'=>$method]);
            break;
            case 'loan':
                $map['id'] = $id;
                D('Loan')->UpdateStatus($map,6);
                $list =  true;
                break;
        }
        if($list){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }











    public function mobile_add(){
      if(IS_POST){
        D('Mobileinfo')->add(I('post.'));
      }
      $this->meta_title = '新增用户';
      $this->display();
    }

    public function bankdelete(){
      $info=D('BankCard')->where("id=".I('get.id'))->find();

      $res=D('Loan')->where("uid=".$info['uid'])->order('`create_time` desc')->find();

      // echo D('Loan')->getlastsql();

      // print_r($res);die;

      if($res['status']==1){
          $trl='您的借款订单已创建，请不要解除绑卡，以免放款失败！';
      }else if($res['status']==2){
          $trl='您的借款订单已处于审核状态，请不要解除绑卡，以免放款失败！';
      }else if($res['status']==4){
          $trl='您的借款订单已处于待打款状态，请不要解除绑卡，以免放款失败！';
      }else if($res['status']==5){
          $trl='您的借款还未还清，请先还款再进行操作！！';
      }else{
          // $info=D('BankCard')->where($res['bank_card'])->find();

          $no_agree=$info['no_agree'];
          //调用解除绑定的方法
          $result=Bankcardunbind($this->uid,$no_agree);
          $result=json_decode($result,true);
          if($result['ret_code']=='0000'){
              //解绑之后删除数据库该行          因暂不支持绑定多张，固可以这么写！
              D('BankCard')->where('uid='.$this->uid)->delete();
              $trl='解绑成功！';

          }else{
              $trl=$result['ret_msg'];
          }
      }

      
      // ob_clean();
      echo $trl;die;
  }


}
